import machine
import time

# 定义控制双路继电器的引脚
relay_pin1 = machine.Pin(2, machine.Pin.OUT)
relay_pin2 = machine.Pin(3, machine.Pin.OUT)
# 定义按钮引脚，启用内部上拉电阻
button_pin = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)

# 定义时间常量（秒）
forward_time = 5
stop_time = 0.5
reverse_time = 5

# 初始状态下，两个继电器都断开
relay_pin1.value(0)
relay_pin2.value(0)

while True:
    # 检测按钮是否被按下
    if button_pin.value() == 0:
        # 延迟一小段时间以消除按键抖动
        time.sleep_ms(20)
        if button_pin.value() == 0:
            # 电机正转
            relay_pin1.value(1)
            relay_pin2.value(0)
            time.sleep(forward_time)

            # 电机停止
            relay_pin1.value(0)
            relay_pin2.value(0)
            time.sleep(stop_time)

            # 电机反转
            relay_pin1.value(0)
            relay_pin2.value(1)
            time.sleep(reverse_time)

            # 电机停止
            relay_pin1.value(0)
            relay_pin2.value(0)

            # 等待按钮释放
            while button_pin.value() == 0:
                pass